package com.cloudsigns.factorymanager2.project.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;

/**
 * Created by tony on 2018/6/9.
 */

public class FileUtil {

    public static String getRealPathFromURI(Context context,Uri contentUri) { //传入图片uri地址
        String textPre = contentUri.toString();//得到图片uri地址字符串

        if (textPre.contains("content://")) { //如果包含有content开头，需要转化为其实际路径，不能用content开头
            String[] proj = {MediaStore.Images.Media.DATA};
            CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
            Cursor cursor = loader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else {
            return textPre;       //如果用file开头，不用转化
        }

//
//        String[] proj = {MediaStore.Images.Media.DATA};
//        CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
//        Cursor cursor = loader.loadInBackground();
//        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//        cursor.moveToFirst();
//        return cursor.getString(column_index);
    }
}
